﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyTestProject;

namespace ParseReuters21578
{
    public enum LEWISSPLIT { TRAIN, TEST, NOTUSED }

    public class Article: ICheckable
    {
        private bool _hasTopics;
        private LEWISSPLIT _lewissplit;
        private string _id;
        private string _author;
        private string _title;
        private string _body;
        private string _date;
        private List<string> _topics;
        private List<string> _places;
        private string _documentName;

        private Dictionary<string,double> _tf;
        private int _maxFrequency;

        public Article(string id, string date, string author, string body, string title,
            List<string> topics, List<string> places, string path, bool hasTopics, LEWISSPLIT lewissplit)
        {
            _id = id;
            _author = author;
            _date = date;
            _body = body;
            _title = title;
            _topics = topics;
            _places = places;
            _documentName = path;
            _hasTopics = hasTopics;
            _lewissplit = lewissplit;

            _tf = new Dictionary<string, double>();
            _maxFrequency = 0;
            GenerateTF();
        }

        private void GenerateTF()
        {
            string[] docArray = _body.Split(' ');
            foreach (string word in docArray)
            {
                if (_tf.ContainsKey(word))
                {
                    _tf[word]++;
                }
                else
                {
                    _tf[word] = 1;
                }

                if (_tf[word] > _maxFrequency)
                {
                    _maxFrequency = (int)_tf[word];
                }
            }

            List<string> keys = _tf.Keys.ToList();
            foreach (string key in keys)
            {
                _tf[key] /= _maxFrequency;
            }
        }

        public List<string> Topics { get { return _topics; } }
        public LEWISSPLIT Lewissplit { get { return _lewissplit; } }
        public bool HasTopics { get { return _hasTopics; } }

        #region ICheckable
        public int ID
        {
            get { return Convert.ToInt16(_id); }
        }

        public string Text
        {
            get { return _body; }
        }

        public Dictionary<string, double> TF
        {
            get { return _tf; }
        }

        public bool Contains(string word)
        {
            if (_body.Split(' ').Contains(word))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int NumberOfOccurrences(string word)
        {
            int ans;
            if (!_body.Split(' ').Contains(word))
            {
                return 0;
            }
            else
            {
                ans = 0;
                string[] strArray = _body.Split(' ');
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i].Equals(word))
                    {
                        ans++;
                    }
                }
            }
            return ans;
        }
        #endregion
    }
}
